#pragma once
#include <assert.h> 
#include "Reverse_iterator.h"
#include <algorithm>
namespace xxx {
	template<class T>
	struct list_node {
		list_node<T>* _next;
		list_node<T>* _prev;
		T _val;
		list_node(const T& val=T()) 
			:_next(nullptr)
			,_prev(nullptr)
			,_val(val)
		{}
	};

	template <class T,class Ref,class Ptr>
	struct list_iterator {
		typedef list_node<T> Node;
		typedef list_iterator<T,Ref,Ptr> self;
		Node* _node;
		list_iterator(Node*node)
			:_node(node)
			{}
		Ref operator* () {
			return _node->_val;
		}
		Ptr operator->() {
			return &_node->_val;
		}
		self& operator++() {
			_node = _node->_next;
			return *this;
		}
		self operator++(int) {
			self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		self& operator--() {
			_node = _node->_prev;
			return *this;
		}
		self operator--(int) {
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const self& it) const {
			return _node != it._node;
		}
		bool operator==(const self& it)const {
			return _node == it._node;
		}

	};


	template<class T>
	class list {
	public:
		typedef list_node<T> Node;
		typedef list_iterator<T,T&,T*> iterator;
		typedef list_iterator<T,const T&,const T*> const_iterator;
	    
		typedef Reverse_iterator<iterator, T&, T*> reverse_iterator;
		typedef Reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
		list() {
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		list(const list<T>& it) {
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			 for(auto& ch:it){
				push_back(ch);
			}
		}
		list<T>& operator=(list<T> l){
			swap(l);
			return *this;
		}

		~list() {
			clear();
			delete _head;
			_head = nullptr;
		}
		
		void push_back(const T& val) {
			insert(end(), val);
		}
		void push_front(const T& val) {
			insert(begin(), val);
		}
		void pop_back() {
			erase(--end());
		}
		void pop_front() {
			erase(begin());
		}
		void clear() {
			auto it = begin();
			while (it != end()) {
				it = erase(it);
			}
			_size = 0;
		}
		iterator begin() {
			return _head->_next;
		}
		iterator end() {
			return _head;
		}
		const_iterator begin() const {
			return _head->_next;
		}
		const_iterator end() const {
			return _head;
		}
		reverse_iterator rbegin() {
			return reverse_iterator(end());
		}
		reverse_iterator rend() {
			return reverse_iterator(begin());
		}
		iterator insert(iterator pos,const T& val) {
			assert(pos >= begin() && pos < end());
			Node* newnode=new Node(val);
			pos._node->_prev->_next = newnode;
			newnode->_prev = pos._node->_prev;

			newnode->_next = pos._node;
			pos._node->_prev = newnode;

			_size++;
			return pos;
		}
		iterator erase(iterator pos) {
			assert(pos >= begin() && pos < end());
			pos._node->_next->_prev = pos._node->_prev;
			pos._node->prev->_next = pos._node->_next;
			delete pos._node;

			--_size;
			return pos._node->_next;
		}
		void swap(list<T>& l) {
			std::swap(_head, l._head);
			std::swap(_size, l._size);
		}
		size_t size() {
			return _size;
		}
	private:
		Node* _head;
		size_t _size=0;
	};
};